﻿using System;
using System.Collections.Generic;
using System.Text;
using Exam.Core.DBUtility;
using System.Data.OleDb;

namespace Exam.Core.DAL {
    public class StudentExam
    {
        /// <summary>
        /// 考生开始考试
        /// </summary>
        /// <returns></returns>
        public int StuBeginExam(int paperid, string myid, string startime, int status)
        {
            //先查询该考生是否已经打开过该试卷
            int resu = 0;
            string SQL = "SELECT StudentExamId FROM StudentExam WHERE PaperId=" + paperid + " AND StudentId=" + myid + "";
            object obj = DbHelperOleDb.GetSingle(SQL);
            if (obj != null)
            {
                if (obj.ToString().Trim() != "")
                {
                    resu = int.Parse(obj.ToString().Trim());
                    if (resu > 0)
                    {
                        return resu;
                    }
                }
            }
            //不存在则插入一条数据 
            SQL = "INSERT INTO StudentExam (PaperId,StudentId,StarTime,ExamStatus) VALUES (" + paperid + "," + myid + ",'" + startime + "'," + status + ")";
            int rows = DbHelperOleDb.ExecuteSql(SQL);
            int result = DbHelperOleDb.GetMaxID("StudentExamId", "StudentExam");
            return result;
        }
        /// <summary>
        /// 考生结束考试
        /// </summary>
        /// <returns></returns>
        public bool StuEndExam()
        {
            bool result = false;


            return result;

        }

        public Exam.Core.Model.StudentExam getStudentScoreByStuidPaperid(int studentid, int paperid)
        {
            string SQL = "SELECT * FROM StudentExam WHERE StudentId=@StudentId AND PaperId=@PaperId";
            OleDbParameter[] parameters = {
					new OleDbParameter("@StudentId", OleDbType.Integer,4),
                    new OleDbParameter("@PaperId", OleDbType.Integer,4)                      
                };
            parameters[0].Value = studentid;
            parameters[1].Value = paperid;

            Exam.Core.Model.StudentExam obj = new Exam.Core.Model.StudentExam();
            using (OleDbDataReader dr = DbHelperOleDb.ExecuteReader(SQL, parameters))
            {
                while (dr.Read()) {
                    if (dr[0] != DBNull.Value) {
                        obj.StudentExamId = dr.GetInt32(0).ToString().Trim();
                    }
                    if (dr[1] != DBNull.Value) {
                        obj.StudentId = dr.GetInt32(1).ToString().Trim();
                    }
                    if (dr[2] != DBNull.Value) {
                        obj.PaperId = dr.GetInt32(2).ToString().Trim();
                    }
                    if (dr[3] != DBNull.Value) {
                        obj.StarTime = dr.GetDateTime(3).ToString().Trim();
                    }
                    if (dr[4] != DBNull.Value) {
                        obj.EndTime = dr.GetValue(4).ToString().Trim();
                    }
                    if (dr[5] != DBNull.Value) {
                        obj.StudentPaper = dr.GetValue(5).ToString().Trim();
                    }
                    if (dr[6] != DBNull.Value) {
                        obj.PaperTotal = dr.GetFloat(6).ToString().Trim();
                    }
                    if (dr[7] != DBNull.Value) {
                        obj.TotalScore = dr.GetFloat(7).ToString().Trim();
                    }
                    if (dr[8] != DBNull.Value) {
                        obj.TotalScorePer = dr.GetFloat(8).ToString().Trim();
                    }
                    if (dr[9] != DBNull.Value) {
                        obj.ExamStatus = dr.GetInt32(9).ToString().Trim();
                    }
                    if (dr[10] != DBNull.Value) {
                        obj.NeedRead = dr.GetInt32(10).ToString().Trim();
                    }
                }
            }
            return obj;
        }


        public System.Data.DataTable getStuScoreList(int id) {
            string SQL = "SELECT StudentExam.*,Student.StudentName FROM StudentExam RIGHT JOIN Student ON StudentExam.StudentID = Student.StudentID WHERE StudentExam.PaperId=" + id + " AND StudentExam.ExamStatus=2";
            return DbHelperOleDb.Query(SQL).Tables[0];
        }
    }


}
